﻿#include "xray_predict_mainwindow.h"
#include "xray_prediction.h"

#include <qlabel.h>
#include <qpushbutton.h>
#include <qboxlayout.h>
#include <qsplitter.h>
#include <qfiledialog.h>
#include <qdebug.h>
#include <qprocess.h>
#include <qthread.h>
#include <qtextbrowser.h>
#include <qmessagebox.h>
#include <qlistwidget.h>
#include <qlineedit.h>



xray_predict_mainwindow::xray_predict_mainwindow(QWidget*)
{
	setMinimumSize(1200, 800);
	setUi();
	initconnection();

}

xray_predict_mainwindow::~xray_predict_mainwindow()
{
	predictThread.quit();
	predictThread.wait();
}

void xray_predict_mainwindow::setUi()
{
	QWidget* mainWidget = new QWidget(this);
	setCentralWidget(mainWidget);
	QHBoxLayout* mainLayout = new QHBoxLayout(mainWidget);
	
	QVBoxLayout* leftLayout = new QVBoxLayout;
	leftLayout->setAlignment(Qt::AlignTop);
	QVBoxLayout* rightLayout = new QVBoxLayout;
	rightLayout->setAlignment(Qt::AlignTop);
	mainLayout->addLayout(leftLayout);
	QFrame* frameline = new QFrame;
	frameline->setFrameShape(QFrame::VLine);

	frameline->setFrameShadow(QFrame::Sunken);

	frameline->raise();//னழூலுரஔூஏமூணுயுடித
	mainLayout->addWidget(frameline);
	mainLayout->addLayout(rightLayout);

    BrowseButton = new QPushButton(u8"上传图片");
	BrowseButton->setMaximumSize(180, 30);
	BrowseButton->setMinimumSize(180, 30);

    PredictButton = new QPushButton(u8"预测结果");
	PredictButton->setMaximumSize(180, 30);
	PredictButton->setMinimumSize(180, 30);

	ResultBrowser = new QTextBrowser;
    personBrowser = new QListWidget;

	rightLayout->addWidget(BrowseButton);
	leftLayout->addWidget(PredictButton);
	leftLayout->addWidget(ResultBrowser);


    QHBoxLayout* selectLayout = new QHBoxLayout;
    nameline = new QLineEdit;
    nameline->setMaxLength(10);
    nameline->setMaximumWidth(100);
    nameButton = new QPushButton(u8"查询");
    nameButton->setMaximumWidth(60);
    uploadData = new QPushButton(u8"上传");
    uploadData->setMaximumWidth(60);

    selectLayout->addWidget(nameline);
    selectLayout->addWidget(nameButton);
    selectLayout->addWidget(uploadData);
    selectLayout->setAlignment(Qt::AlignLeft);
    leftLayout->addLayout(selectLayout);
    leftLayout->addWidget(personBrowser);

	XRay_pic = new QLabel;
	XRay_pic->setMaximumSize(350, 350);
	XRay_pic->setMinimumSize(350, 350);
	QImage* default_pic = new QImage;
	default_pic->load(":/xray_predict_mainwindow/350.jpg");
	XRay_pic->setPixmap(QPixmap::fromImage(*default_pic));
	rightLayout->addWidget(XRay_pic);

}

void xray_predict_mainwindow::initconnection()
{
	connect(BrowseButton,SIGNAL(clicked()),this,SLOT(browseXRaySlot()));
	connect(PredictButton, SIGNAL(clicked()), this, SLOT(predictionSlot()));
    connect(nameButton,SIGNAL(clicked()),this,SLOT(selectNameSlot()));
    connect(uploadData,SIGNAL(clicked()),this,SLOT(uploadSlot()));

	xray_prediction* prediction = new xray_prediction;
	
	prediction->moveToThread(&predictThread);
	connect(&predictThread, SIGNAL(QThread::finished()), prediction, SLOT(QObject::deleteLater()));
	connect(this, SIGNAL(startPredictSignal(const QString&)), prediction, SLOT(prediction(const QString&)));
	connect(prediction, SIGNAL(predictResult(const QString&)), this, SLOT(showPredictResult(const QString&)));
	predictThread.start();

//    db = QSqlDatabase::addDatabase("QODBC");
//            db.setHostName("127.0.0.1");
//            db.setPort(3306);
//            db.setDatabaseName("1");
//            db.setUserName("root");
//            db.setPassword("admin");
//            bool ok = db.open();
//            if (ok){
//                QMessageBox::information(this, "infor", "success");

//            }
//            else {
                //QMessageBox::information(this, "infor", "open failed");
                //qDebug()<<"error open database because"<<db.lastError().text();
            //}

            //ODBC查询数据
//                QSqlQuery result = db.exec("select * from patient where name=""");
//                while(result.next()){
//                    qDebug()<<"user_id:"<<result.value("id").toString();
//                    qDebug()<<"name:"<<result.value("name").toString();
//                    qDebug()<<"sex:"<<result.value("sex").toString();
//                    qDebug()<<"age:"<<result.value("age").toInt()<<endl;
//                }
           // QTextCodec*t=QTextCodec::codecForName("GBK");
            //qDebug() << QString::fromLocal8Bit("insert into patient(id,name,sex,age) values ( '420106200002243638','曹军','male',22)");
                //QSqlQuery result;
                //bool result_successed = result.exec(QString::fromLocal8Bit("insert into patient(id,name,sex,age) values ('420106200002243638','曹军','male',22)"));
}

void xray_predict_mainwindow::selectNameSlot()
{
    if(nameline->text().isEmpty())
        QMessageBox::information(this,"info",QString::fromLocal8Bit("请输入查询名称"));
    else
    {
        personBrowser->clear();
        db = QSqlDatabase::addDatabase("QODBC");
                db.setHostName("127.0.0.1");
                db.setPort(3306);
                db.setDatabaseName("1");
                db.setUserName("root");
                db.setPassword("admin");
        bool ok = db.open();
                   if (ok){
                        QMessageBox::information(this, "infor", "success");

                    }
                    else {
                        QMessageBox::information(this, "infor", "open failed");
                        qDebug()<<"error open database because"<<db.lastError().text();
                    }
                   QString sql = QString("select * from patient where name=\"").append(nameline->text()) + "\"";

        QSqlQuery result = db.exec(sql);
                        while(result.next()){
                           qDebug()<<"user_id:"<<result.value("id").toString();
                            qDebug()<<"name:"<<result.value("name").toString();
                            qDebug()<<"sex:"<<result.value("sex").toString();
                            qDebug()<<"age:"<<result.value("age").toInt()<<endl;

                            personBrowser->addItem(QString::fromLocal8Bit("身份证:") + result.value("id").toString() +
                                                   QString::fromLocal8Bit("\t姓名:") + result.value("name").toString() +
                                                   QString::fromLocal8Bit("\t性别:") + result.value("sex").toString() +
                                                   QString::fromLocal8Bit("\t年龄:") + result.value("age").toString()
                                                   );
                       }
    }
}

void xray_predict_mainwindow::browseXRaySlot()
{
	XRayRoot = QFileDialog::getOpenFileName(this, tr("open"), ".", "(*jpg *jpeg *png)");
	//qDebug() << XRayRoot;
	QImage* pic = new QImage;
	pic->load(XRayRoot);
	XRay_pic->setPixmap(QPixmap::fromImage(*pic).scaled(XRay_pic->size()));
}

void xray_predict_mainwindow::uploadSlot()
{
    int ref = QMessageBox::question(this, u8"确认",
                                    u8"确认上传吗？", QMessageBox::Yes, QMessageBox::No);
    if(ref == QMessageBox::Yes)
    {
        auto currentitem = personBrowser->currentItem();

        QSqlQuery result;
        if(gailv.toDouble() < 0.05)
        {
            QString sql = QString::fromLocal8Bit("insert into recode(patient_id,picture,first_check,second_check,xray_doctor_id) values ('420106200002243638','").append(pic_root + "','").append(u8"患病概率:" + gailv).append(u8"','患病概率低，跳过复审',").append(currentId + ");");
            qDebug() << sql;
            bool result_successed = result.exec(sql);
            qDebug() << result_successed;
        }
    }
}

void xray_predict_mainwindow::predictionSlot()
{
	if (!XRayRoot.isEmpty())
	{
		emit startPredictSignal(XRayRoot);
		PredictButton->setEnabled(false);
	}
}

void xray_predict_mainwindow::showPredictResult(const QString& result)
{
	qDebug() << result;
	PredictButton->setEnabled(true);
	QStringList list = result.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
	qDebug() << list;
    pic_root = list[0];
    gailv = list[2];
    predict_result = list[3];
	ResultBrowser->append(result);
}
